package com.bwsoft.action;

import com.bwsoft.domain.User;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

public class FunAction extends BaseAction {

	private static final long serialVersionUID = 833733815845359726L;
	private static Logger log = Logger.getLogger(FunAction.class);

	/**
	 * 展示所有菜单列表
	 */
	public String ShowList() {
		log.info("用户：" + ((User) session.get("User")).getName() + "访问【" + fun.getFunName() + "】");
		funList = funService.getFunList();
		return "list";
	}

	/**
	 * 2.0 添加菜单
	 */
	@SuppressWarnings("unchecked")
	public String addFun() {

		// 表单验证
		if (fun.getParentFunID().equals("-1")) {
			responseJson.put("result", "Fail");
			responseJson.put("message", "请选择父节点！");
			return "mapJsonResult";
		}
		if (fun.getFunName().isEmpty() || fun.getFunName() == "") {
			responseJson.put("result", "Fail");
			responseJson.put("message", "请填写节点名称！");
			return "mapJsonResult";
		}
		if (fun.getFunUrl().isEmpty() || fun.getFunUrl() == "") {
			responseJson.put("result", "Fail");
			responseJson.put("message", "请填写节点URL！");
			return "mapJsonResult";
		}

		// 将fun对象添加到数据库
		msg = funService.addFun(fun);
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());

		return "mapJsonResult";

	}

	/**
	 * 删除菜单
	 */
	public String delete() {
		String idString = (String) request.getParameter("idString");
		// Map：装载Json对象
		Map<String, Object> map = new HashMap<String, Object>();

		// 判断删除的ID是否为空
		if (idString.isEmpty() || idString == "") {
			map.put("result", "Fail");
			map.put("message", "请选择要删除的节点！");
			responseJson = map;
			return "delete";
		}

		// 删除
		if (funService.deleteFun(idString)) {
			map.put("result", "Success");
			map.put("message", "操作成功！");
		} else {
			map.put("result", "Fail");
			map.put("message", "操作失败！");
		}
		responseJson = map;
		return "mapJsonResult";
	}

	/**
	 * 4.0 查询要修改Fun对象
	 */

	public String findFun() {

		// Map：装载Json对象
		Map<String, Object> map = new HashMap<String, Object>();

		// 根据ID查询Fun对象
		fun = funService.findById(fun.getFunID());

		// 将查询到的Fun对象放入JSON容器
		map.put("Fun", fun);
		responseJson = map;

		return "mapJsonResult";
	}

	/**
	 * 5.0 更新Fun
	 * 
	 * @return
	 */

	public String updateFun() {
		// Map：装载Json对象
		Map<String, Object> map = new HashMap<String, Object>();

		// 表单验证
		if (fun.getParentFunID().equals("-1")) {
			map.put("result", "Fail");
			map.put("message", "请选择父节点！");
			responseJson = map;
			return "mapJsonResult";
		}
		if (fun.getFunName().isEmpty() || fun.getFunName() == "") {
			map.put("result", "Fail");
			map.put("message", "请填写节点名称！");
			responseJson = map;
			return "mapJsonResult";
		}
		if (fun.getFunUrl().isEmpty() || fun.getFunUrl() == "") {
			map.put("result", "Fail");
			map.put("message", "请填写节点URL！");
			responseJson = map;
			return "mapJsonResult";
		}

		if (funService.updateFun(fun)) {
			map.put("result", "Success");
			map.put("message", "操作成功！");
		} else {
			map.put("result", "Fail");
			map.put("message", "操作失败！");
		}

		responseJson = map;
		return "mapJsonResult";
	}

	/**
	 * 6.0 上移菜单
	 * 
	 * @return
	 */
	public String moveUpFun() {
		// Map：装载Json对象
		Map<String, Object> map = new HashMap<String, Object>();

		msg = funService.gotoUp(fun.getFunID());

		// 将Json内容放入Map容器
		if (msg.getMsgFlag()) {
			map.put("result", "Success");
			map.put("message", "操作成功！");
		} else {
			map.put("result", "Fail");
			map.put("message", msg.getMsgStr());
		}
		responseJson = map;
		return "mapJsonResult";
	}

	/**
	 * 7.0 下移菜单
	 * 
	 * @return
	 */
	public String moveDownFun() {
		// Map：装载Json对象
		Map<String, Object> map = new HashMap<String, Object>();

		msg = funService.gotoDown(fun.getFunID());

		// 将Json内容放入Map容器
		if (msg.getMsgFlag()) {
			map.put("result", "Success");
			map.put("message", "操作成功！");
		} else {
			map.put("result", "Fail");
			map.put("message", msg.getMsgStr());
		}
		responseJson = map;
		return "mapJsonResult";
	}

}
